<template>
  <div>
    <el-row>
      <el-col>
        <div class="search_wrap">
          <el-form
            :inline="true"
            :model="filters"
            class="demo-form-inline"
            ref="searchForm"
          >
            <el-form-item prop="alarmState">
              <el-select
                v-model="filters.alarmState"
                clearable
                filterable
                placeholder="报警处理状态"
              >
                <el-option
                  v-for="item in alarmStateList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="allTime">
              <el-date-picker
                v-model="filters.allTime"
                :clearable="true"
                type="datetimerange"
                range-separator="至"
                start-placeholder="查询开始日期"
                end-placeholder="查询结束日期"
                :default-time="['00:00:00', '23:59:59']"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-left" @click="resetForm"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
    <el-table
      :data="tableList"
      :fit="true"
      highlight-current-row
      v-loading="listLoading"
      element-loading-text="数据加载中，请稍后..."
      empty-text="对不起，没有找到相关数据"
    >
      <el-table-column type="index" label="序号" align="center" width="100px">
      </el-table-column>
      <el-table-column
        prop="equipName"
        label="设备名称"
        width="150px"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="equipCode"
        label="设备编号"
        width="150px"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="alarmTrigger"
        label="触发类型"
        width="150px"
        align="center"
      >
        <template slot-scope="scope">
          <div
            :style="{ color: scope.row.alarmTrigger === 1 ? 'red' : 'green' }"
          >
            {{ formatAlarmTrigger(scope.row.alarmTrigger) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="alarmType"
        label="报警类型 "
        width="150px"
        align="center"
      >
        <template slot-scope="scope">
          <div style="color: red;">
            {{ formatAlarmType(scope.row.alarmType) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="content"
        label="报警内容"
        width="200px"
        align="center"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.content }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="alarmState"
        label="处理状态"
        width="150px"
        align="center"
      >
        <template slot-scope="scope">
          <div :style="{ color: scope.row.alarmState === 1 ? 'red' : 'green' }">
            {{ formatAlarmState(scope.row.alarmState) }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="createDate"
        label="报警时间"
        width="auto"
        align="center"
      >
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.pageNo"
      :page-sizes="[20, 50, 100]"
      :page-size="page.pageSize"
      layout="total, sizes, jumper, next, pager, prev"
      :total="page.pageTotal"
    ></el-pagination>
  </div>
</template>
<script>
import SmokeApi from "@/api/equip/smoke";
export default {
  props: {
    equipId: Number | String,
    typeName:String
  },
  data() {
    return {
      dialog: {
        open: false, // 默认隐藏
        title: ""
      },
      new_equipId: "",
      loading: false,
      tableList: [],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20
      },
      filters: {
        alarmState: "",
        allTime: []
      },
      listLoading: true,
      alarmStateList: [{ id: 1, name: "报警中" }, { id: 2, name: "已处理" }]
    };
  },
  created() {
    this.new_equipId = this.equipId;
    this.init();
  },
  watch: {
    equipId(newVal, oldVal) {
      this.new_equipId = newVal;
      if(this.typeName=='alarm'){
        this.getData();
      }
    },
    typeName(newVal){
      if(newVal=='alarm'){
        this.getData();
      }
    }
  },
  methods: {
    init() {
      this.$Utils.clearData(this.tableList);
    },
    // handleCreate(val) {
    //     // 父组件 调用子组件
    //     this.dialog.open = true; // 显示弹出框
    //     this.dialog.title = "报 警 记 录";
    //     this.equipId = val
    //     this.getData()
    // },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch()
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.getData();
    },
    getData() {
      var cond = {
        equipId: this.new_equipId,
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        alarmState: this.filters.alarmState
      };
      if (this.filters.allTime && this.filters.allTime.length > 0) {
        cond.beginDate = this.$Utils.formatDate(
          this.filters.allTime[0],
          "yyyy-MM-dd hh:mm:ss"
        );
        cond.endDate = this.$Utils.formatDate(
          this.filters.allTime[1],
          "yyyy-MM-dd hh:mm:ss"
        );
        this.$Utils.formatDate(this.filters.allTime[0], "yyyy-MM-dd hh:mm:ss");
      }
      this.loading = true;
      SmokeApi.getAlarmLogPage(cond).then(res => {
        this.page.pageTotal = res.data.total;
        this.tableList = res.data.list;
        this.listLoading = false;
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.getData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.getData();
    },
    formatAlarmTrigger(cellValue) {
      if (cellValue === 1) {
        return "报警";
      } else if (cellValue === 0) {
        return "恢复";
      }
    },
    formatAlarmState(cellValue) {
      if (cellValue === 1) {
        return "报警中";
      } else if (cellValue === 2) {
        return "已处理";
      }
    },
    formatAlarmType(cellValue) {
      switch (cellValue) {
        case "fire":
          return "火警";
        case "gasLeak":
          return "燃气泄漏";
        case "lockOpen":
          return "异常开门";
        case "soak":
          return "水浸溢水";
        case "sos":
          return "紧急求助";
        case "fall":
          return "跌倒求救";
        case "help":
          return "120求救";
        case "infra":
          return "红外异常";
        case "body":
          return "有人报警";
        case "nobody":
          return "无人报警";
        case "sleep":
          return "睡眠异常";
        case "highSugar":
          return "高糖报警";
        case "lowSugar":
          return "低糖报警";
        case "highPressure":
          return "高压报警";
        case "normal":
          return "报警恢复";
        case "lowPower":
          return "低电报警";
        case "open":
          return "防拆报警";
        case "trouble":
          return "故障报警";
        case "test":
          return "测试报警";
        case "temp":
          return "温度报警";
        case "error":
          return "异常警告";
        default:
          return "";
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.rotate-cartoon {
  animation: cartoon 0.5s linear 0s infinite backwards;
  // -webkit-animation:cartoon .3s linear 0  infinite
}

@keyframes cartoon {
  0% {
    transform: rotate(0);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>
